package com.hua.nailcampus.acl.controller;

import com.hua.nailcampus.acl.model.Avatar;
import com.hua.nailcampus.acl.model.Friends;
import com.hua.nailcampus.acl.model.User;
import com.hua.nailcampus.acl.model.vo.FriendsVo;
import com.hua.nailcampus.acl.model.vo.UserVo;
import com.hua.nailcampus.acl.service.AvatarService;
import com.hua.nailcampus.acl.service.FriendsService;
import com.hua.nailcampus.acl.service.MessagesService;
import com.hua.nailcampus.acl.service.UserService;
import com.hua.nailcampus.utils.JwtHelper;
import com.hua.nailcampus.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author:chenjianhua
 * @Date:2023/11/21 18:47
 * @Version 1.8
 */
@Slf4j
@RestController
@RequestMapping("nailCampus/friend")
public class FriendController {

    @Autowired
    private FriendsService friendsService;

    @Autowired
    private UserService userService;

    @Autowired
    private MessagesService messagesService;

    /**
     * 申请添加好友
     * @param friends
     * @return
     */
    @PostMapping("/addFriend")
    public Result addFriend(@RequestBody Friends friends){
//        因为两个用户互相为好友，所以要互换ID存入数据库，方便后面的昵称设置
        friendsService.addFriend(friends);
        return Result.ok(null);
    }

    /**
     * 申请通过
     * @return
     */
    @PostMapping("/passAdd")
    public Result passAdd(@RequestBody Friends friends){
        log.info("friend:{}",friends.toString());
//        同意好友同意之后，两人称为好友关系
        friendsService.passAdd(friends);
        return Result.ok(null);
    }


    /**
     * 根据好友ID获取申请消息
     * @param request
     * @param fid
     * @return
     */
    @GetMapping("/getFriends/{fid}")
    public Result getFriends(HttpServletRequest request,
                             @PathVariable Integer fid){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        Friends friends = friendsService.getaddFriends(userId,fid);
        return Result.ok(friends);
    }

    /**
     * 获取用户所有好友申请消息
     * @param request
     * @return
     */
    @GetMapping("/getAllAddMessage")
    public Result getAllAddMessage(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer id = JwtHelper.getUserId(token);
        List<FriendsVo> friendsVoList = friendsService.getFriendAddMessage(id);
        return Result.ok(friendsVoList);
    }

    /**
     * 查询两个用户是否为好友关系
     * @param request
     * @param fid
     * @return
     */
    @GetMapping("/getRelationship/{fid}")
    public Result selectRelationship(HttpServletRequest request
                                , @PathVariable Integer fid){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        log.info("userId:{},fId:{}",userId,fid);
        Boolean isFriend = friendsService.getRelationship(userId, fid);
        if (!isFriend){
            return Result.fail(null);
        }
        return Result.ok(null);
    }

    /**
     * 删除好友
     * @param request
     * @param fId
     * @return
     */
    @PostMapping("/removeFriend")
    public Result removeFriend(HttpServletRequest request,
                               @RequestParam("fId") Integer fId){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        log.info("删除好友，userId={},fId={}",userId,fId);
//        删除自己跟好友的数据
        friendsService.removeFriendById(userId, fId);
//        删除聊天记录
        messagesService.removeMessagesById(userId,fId);
        messagesService.removeMessagesById(fId,userId);
        return Result.ok("删除成功");
    }

    /**
     * 获取好友列表
     * @param request
     * @return
     */
    @GetMapping("/getFriendList")
    public Result getFriendList(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        List<UserVo> userVoList = friendsService.getFriendListById(userId);
        return Result.ok(userVoList);
    }

    @PostMapping("/updateNickName")
    public Result updateNickname(HttpServletRequest request,
                                 @RequestParam("fId") Integer fId,
                                 @RequestParam("nickName") String nickName){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        friendsService.updateNickName(userId,fId,nickName);
        return Result.ok(null);
    }

}
